
from PyQt5.QtWidgets import QLabel, QLineEdit, QVBoxLayout, QWidget, QHBoxLayout, QFrame
from signal_mgr import get_signal_manager
from data.data_mgr import get_data_manager
from util import analysis
import define

class CPathLine(QLineEdit):
    """实现文件拖放功能"""

    def __init__(self):
        super().__init__()
        self.setAcceptDrops(True) # 设置接受拖放动作
        self.setReadOnly(True)
        self.setPlaceholderText('将.pro文件拖拽到此即可')

    def dragEnterEvent(self, e):
        if e.mimeData().text().endswith('.pro'): # 如果是.srt结尾的路径接受
            e.accept()
        else:
            e.ignore()

    def dropEvent(self, e): # 放下文件后的动作
        path = e.mimeData().text().replace('file:///', '') # 删除多余开头
        self.setText(path)



class CToolBar(QFrame):
    """上面的工具栏"""
    def __init__(self):
        super().__init__()
        self._init_widget()
        self._init_connect()

    def _init_widget(self):
        """初始化控件"""
        self.setFrameShape(QFrame.StyledPanel)
        self.setMaximumHeight(100)
        self.top = QWidget()
        self.bottom = QWidget()
        vbox = QVBoxLayout(self)
        vbox.addWidget(self.top)
        vbox.addWidget(self.bottom)
        hbox1 = QHBoxLayout()
        hbox2 = QHBoxLayout()
        self.top.setLayout(hbox1)
        self.bottom.setLayout(hbox2)

        self.lable_file_name = QLabel('文件名')
        self.lable_function_num = QLabel('涉及函数个数')
        self.lable_function_max_consuming_time = QLabel('函数最高耗时(s)')
        self.lable_sentence_max_consuming_time = QLabel('语句最高耗时(s)')
        self.lable_sentence_max_time = QLabel('最高调用次数')

        self.lineedit_file_name = CPathLine()
        self.lineedit_function_num = QLineEdit('')
        self.lineedit_function_num.setReadOnly(True)
        self.lineedit_function_max_consuming_time = QLineEdit('')
        self.lineedit_function_max_consuming_time.setReadOnly(True)
        self.lineedit_sentence_max_consuming_time = QLineEdit('')
        self.lineedit_sentence_max_consuming_time.setReadOnly(True)
        self.lineedit_sentence_max_time = QLineEdit('')
        self.lineedit_sentence_max_time.setReadOnly(True)

        hbox1.addWidget(self.lable_file_name)
        hbox1.addWidget(self.lineedit_file_name)

        hbox2.addWidget(self.lable_function_num)
        hbox2.addWidget(self.lineedit_function_num)
        hbox2.addWidget(self.lable_function_max_consuming_time)
        hbox2.addWidget(self.lineedit_function_max_consuming_time)
        hbox2.addWidget(self.lable_sentence_max_consuming_time)
        hbox2.addWidget(self.lineedit_sentence_max_consuming_time)
        hbox2.addWidget(self.lable_sentence_max_time)
        hbox2.addWidget(self.lineedit_sentence_max_time)


    def _init_connect(self):
        """初始化信号"""
        self.lineedit_file_name.textChanged.connect(self._on_change_file_name)
        

    def _on_change_file_name(self):
        """更换文件"""
        data_info = analysis.analysis_file(self.lineedit_file_name.text())
        data_mgr = get_data_manager()
        data_mgr.chunk_list = data_info.get(define.CHUNK_LIST, [])
        data_mgr.max_function_consuming_time = data_info.get(define.MAX_FUNCTION_CONSUMING_TIME, 0)
        data_mgr.sentence_max_consuming_time = data_info.get(define.SENTENCE_MAX_CONSUMING_TIME, 0)
        data_mgr.sentence_max_time = data_info.get(define.SENTENCE_MAX_TIME, 0)
        self.lineedit_function_num.setText(str(len(data_mgr.chunk_list)))
        self.lineedit_function_max_consuming_time.setText(str(data_mgr.max_function_consuming_time))
        self.lineedit_sentence_max_consuming_time.setText(str(data_mgr.sentence_max_consuming_time))
        self.lineedit_sentence_max_time.setText(str(int(data_mgr.sentence_max_time)))
        get_signal_manager().SIGNAL_PATH_CHANGE.emit()